package com.budwk.app.sqfw.services.impl;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.common.config.AppException;
import com.budwk.app.sqfw.services.HuaweiInterfaceService;
import com.huawei.push.android.*;
import com.huawei.push.exception.HuaweiMesssagingException;
import com.huawei.push.message.AndroidConfig;
import com.huawei.push.message.Message;
import com.huawei.push.message.Notification;
import com.huawei.push.messaging.HuaweiApp;
import com.huawei.push.messaging.HuaweiMessaging;
import com.huawei.push.model.Importance;
import com.huawei.push.model.Urgency;
import com.huawei.push.model.Visibility;
import com.huawei.push.reponse.SendResponse;
import com.huawei.push.util.InitAppUtils;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;


/**
 * @title: HuaweiPushServiceImpl
 * @Author Tan
 * @Date: 2023/7/25 17:10
 * @Version 1.0
 */
@IocBean
@Slf4j
public class HuaweiInterfaceServiceImpl implements HuaweiInterfaceService {

    @Inject
    private PropertiesProxy propertiesProxy;

    @Override
    public SendResponse sendhuaweinotifymessage(String token,String messageContent,String sqfwId,String action, BadgeNotification badgeNotification){
        HuaweiApp app = InitAppUtils.initializeApp();
        HuaweiMessaging huaweiMessaging = HuaweiMessaging.getInstance(app);

        Notification notification = Notification.builder().setTitle("祥云小助")
                .setBody(messageContent)
                .build();

        JSONObject multiLangKey = new JSONObject();
        JSONObject titleKey = new JSONObject();
        titleKey.put("en","好友请求");
        JSONObject bodyKey = new JSONObject();
        bodyKey.put("en","My name is %s, I am from %s.");
        multiLangKey.put("key1", titleKey);
        multiLangKey.put("key2", bodyKey);

        LightSettings lightSettings = LightSettings.builder().setColor(Color.builder().setAlpha(0f).setRed(0f).setBlue(1f).setGreen(1f).build())
                .setLightOnDuration("3.5")
                .setLightOffDuration("5S")
                .build();

        AndroidNotification androidNotification = AndroidNotification.builder().setIcon("/raw/ic_launcher2")
                .setColor("#AACCDD")
                .setSound("/raw/shake")
                .setDefaultSound(true)
                //.setTag("tagBoom")
                .setClickAction(ClickAction.builder().setType(1)
                        .setIntent("intent:#Intent;action="+action+";launchFlags=0x34000000;launchHwFlags=0x100;component=com.dfsjsoft.communityassistant/.ui.main.MainActivity;S."+action+"="+sqfwId+";end").build())
                .setBodyLocKey("key2")
                .addBodyLocArgs("boy").addBodyLocArgs("dog")
                .setTitleLocKey("key1")
                .addTitleLocArgs("Girl").addTitleLocArgs("Cat")
                .setChannelId("NotificationChannelImportantId")
/*
                .setNotifySummary("some summary")
*/
                .setMultiLangkey(multiLangKey)
                .setStyle(0)
                .setBigTitle("祥云小助")
                //消息内容
                .setBigBody(messageContent)
                .setAutoClear(86400000)
                //.setNotifyId(486)
                //.setGroup("com.dfsjsoft.communityassistant.GROUP_KEY_MESSAGE_RECEIVED")
                .setImportance(Importance.NORMAL.getValue())
                .setLightSettings(lightSettings)
                .setBadge(badgeNotification)
                .setVisibility(Visibility.PUBLIC.getValue())
                .setForegroundShow(true)
                .addInboxContent("content1").addInboxContent("content2").addInboxContent("content3").addInboxContent("content4").addInboxContent("content5")
                /*.addButton(Button.builder().setName("button1").setActionType(0).build())
                .addButton(Button.builder().setName("button2").setActionType(1).setIntentType(0).setIntent("https://com.huawei.hms.hmsdemo/deeplink").build())
                .addButton(Button.builder().setName("button3").setActionType(4).setData("your share link").build())*/
                .build();

        AndroidConfig androidConfig = AndroidConfig.builder().setCollapseKey(-1)
                .setUrgency(Urgency.HIGH.getValue())
                .setTtl("10000s")
                .setBiTag("the_sample_bi_tag_for_receipt_service")
                .setNotification(androidNotification)
                .setCategory("WORK")
                .build();

        Message message = Message.builder()
                .setNotification(notification)
                .setAndroidConfig(androidConfig)
                .addToken(token)
                .build();
        try {
            return huaweiMessaging.sendMessage(message);
        } catch (HuaweiMesssagingException e) {
            log.error("push huawei error: ",e);
            throw new AppException();
        }
    }
}
